package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("travel")
@Controller
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") TravelQuery qo){
        Page<Travel> page = travelService.queryPageList(qo);
        model.addAttribute("page",page);
        return "travel/list";
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){
        if(StringUtils.hasLength(travel.getId())){
            travelService.update(travel);
        }else{
            travelService.save(travel);
        }
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        travelService.delete(id);
        return JsonResult.success();
    }

    /**
     * 审核操作
     * @param id
     * @return
     */
    @RequestMapping("audit")
    @ResponseBody
    public Object audit(String id , int state ){
        travelService.audit(id,state);
        return JsonResult.success();
    }

    /**
     * 查看功能
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public Object getContentById(String id){
        Travel travel = travelService.getContentById(id);
        return JsonResult.success(travel.getContent());
    }
}
